<?php

namespace App\Http\Controllers;

use App\Models\User;
use App\Services\FollowService;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class FollowController extends Controller
{
    public function __construct(
        private FollowService $followService
    ) {
        $this->middleware('auth');
    }

    /**
     * 关注/取消关注用户
     */
    public function toggle(User $user): RedirectResponse
    {
        try {
            $currentUser = request()->user();

            if ($currentUser->isFollowing($user)) {
                $this->followService->unfollow($currentUser, $user);
                $message = '已取消关注';
            } else {
                $this->followService->follow($currentUser, $user);
                $message = '关注成功';
            }

            return back()->with('success', $message);
        } catch (\Exception $e) {
            return back()->withErrors(['error' => $e->getMessage()]);
        }
    }

    /**
     * 获取用户的关注列表
     */
    public function following(User $user): Response
    {
        $followings = $this->followService->getFollowingList($user, 20);
        $currentUser = auth()->user();

        // 为每个关注用户标记当前登录用户的关注状态
        if ($currentUser) {
            $followings->getCollection()->transform(function ($following) use ($currentUser) {
                $following->is_following = $currentUser->isFollowing($following);
                $following->followers_count = $following->followers_count ?? $following->followers()->count();
                $following->following_count = $following->following_count ?? $following->followings()->count();
                $following->posts_count = $following->posts()->count();

                return $following;
            });
        }

        return Inertia::render('Users/Following', [
            'user' => [
                'id' => $user->id,
                'username' => $user->username,
                'name' => $user->name,
                'nickname' => $user->nickname,
                'avatar' => $user->avatar,
            ],
            'followings' => $followings,
        ]);
    }

    /**
     * 获取用户的粉丝列表
     */
    public function followers(User $user): Response
    {
        $followers = $this->followService->getFollowersList($user, 20);
        $currentUser = auth()->user();

        // 为每个粉丝标记当前登录用户的关注状态
        if ($currentUser) {
            $followers->getCollection()->transform(function ($follower) use ($currentUser) {
                $follower->is_following = $currentUser->isFollowing($follower);
                $follower->followers_count = $follower->followers_count ?? $follower->followers()->count();
                $follower->following_count = $follower->following_count ?? $follower->followings()->count();
                $follower->posts_count = $follower->posts()->count();

                return $follower;
            });
        }

        return Inertia::render('Users/Followers', [
            'user' => [
                'id' => $user->id,
                'username' => $user->username,
                'name' => $user->name,
                'nickname' => $user->nickname,
                'avatar' => $user->avatar,
            ],
            'followers' => $followers,
        ]);
    }
}
